package com.report.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.report.dto.request.PartnerReq;
import com.report.dto.response.PartnerRes;
import com.report.entity.Partner;
import com.report.mapper.PartnerMapper;
import com.report.service.PartnerService;
import com.zmh.framework.oss.OssConfigure;
import com.zmh.framework.util.Base64ToMultipartFile;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author summer
 * @date 2024-06-28 11:34
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper, Partner> implements PartnerService {

    private final OssConfigure ossConfigure;

    @Override
    public void savePartner(PartnerReq partnerReq) {
        Partner convert = Convert.convert(Partner.class, partnerReq);

        if(partnerReq.getShowImage()){
            MultipartFile multipartFile = Base64ToMultipartFile.base64ToMultipart(partnerReq.getBase64());
            String image = ossConfigure.uploadFile(multipartFile);
            convert.setImage(ossConfigure.getAccessUrl()+image);
        }
         this.saveOrUpdate(convert);
    }

    @Override
    public Page<PartnerRes> lists(PartnerReq partnerReq) {
        Page<Partner> page=new Page<>(partnerReq.getPageNum(),partnerReq.getPageSize());
        LambdaQueryWrapper<Partner> eq = Wrappers.lambdaQuery(Partner.class).eq(StrUtil.isNotEmpty(partnerReq.getName()), Partner::getName, partnerReq.getName())
                .orderByDesc(Partner::getCreateTime);
        page=this.page(page,eq);
        return Convert.convert(new TypeReference<Page<PartnerRes>>() {},page);
    }
}
